import { createRouter, createWebHistory } from 'vue-router'

// 布局组件
import MainLayout from '../layouts/MainLayout.vue'

// 路由配置
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/',
    component: MainLayout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('../views/home/index.vue'),
        meta: { title: '首页', requiresAuth: true }
      }
    ]
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    redirect: '/login'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title || '淘票票系统'
  
  // 检查是否需要登录认证
  if (to.meta.requiresAuth) {
    // 这里可以添加登录验证逻辑，例如检查localStorage中是否有token
    const token = localStorage.getItem('token')
    if (!token) {
      next({ name: 'Login' })
      return
    }
  }
  
  next()
})

export default router 